package com.wss.controller;

import com.alibaba.fastjson.JSON;
import com.wss.domain.Account;
import com.wss.domain.Message;
import com.wss.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value="/toLoginUser")
    public String loginUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
        System.out.println("==============进入到/toLoginUser=============");
        return "loginUser";
    }

    @RequestMapping(value="/toLoginAdmin")
    public String loginAdmin(HttpServletRequest request, HttpServletResponse response) throws Exception{
        System.out.println("==============进入到/toLoginAdmin=============");
        return "loginAdmin";
    }

    /**
     * 用户登录
     * 加入produces参数解决前端ajax接受汉字乱码问题
     * @ ResponseBody将会使这个方法的返回值不再被过滤器解析为请求路径
     * 而是返回给前端的ajax
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/loginCheck", method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String loginCheck(@RequestParam("username") String username,
                             @RequestParam("password") String password,
                             HttpServletRequest request, HttpServletResponse response) throws Exception{
        System.out.println("==============进入到/loginCheck=============");
        System.out.println("==============用户名============="+username);
        Message message = new Message();
        String msg = "";
        String loginOK = "loginOK";
        Map res = loginService.loginSelect(username,password);
        if ((Boolean) res.get(loginOK)){
            List<Account> accountList = (List) res.get("accountList");
            //从list中拿到用户对象
            Account account = accountList.get(0);
            HttpSession session = request.getSession();
            session.setAttribute("Account",account);
            System.out.println("==============登陆成功=============");
            message.setResCode(1);
            message.setResultMsg("登陆成功");
        }else {
            System.out.println("==============登陆失败=============");
            message.setResCode(0);
            message.setResultMsg("登陆失败，用户名密码错误");
        }
        msg = JSON.toJSONString(message);
        System.out.println(msg);
        return msg;
    }

    /**
     * 管理员登录
     * 判断用户权限id是否为1
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/loginAdminCheck", method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String loginAdminCheck(@RequestParam("username") String username,
                                  @RequestParam("password") String password,
                                  HttpServletRequest request, HttpServletResponse response) throws Exception{
        System.out.println("==============进入到/loginAdminCheck=============");
        System.out.println("==============用户名============="+username);
        Message message = new Message();
        String msg = "";
        String loginOK = "loginOK";
        Map res = loginService.loginSelect(username,password);
        if ((Boolean) res.get(loginOK)){
            List<Account> accountList = (List) res.get("accountList");
            //从list中拿到用户对象
            Account account = accountList.get(0);
            if (account.getP_id() == 1) {
                HttpSession session = request.getSession();
                session.setAttribute("Account", account);
                System.out.println("==============登陆成功=============");
                message.setResCode(1);
                message.setResultMsg("登陆成功");
            }else{
                System.out.println("==============登陆失败，非管理员用户=============");
                message.setResCode(0);
                message.setResultMsg("登陆失败，非管理员用户");
            }
        }else {
            System.out.println("==============登陆失败=============");
            message.setResCode(0);
            message.setResultMsg("登陆失败，用户名密码错误");
        }
        msg = JSON.toJSONString(message);
        System.out.println(msg);
        return msg;
    }

    /**
     * 退出登陆清除用户session
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/userLoginOut")
    public String userLoginOut(HttpServletRequest request, HttpServletResponse response) throws Exception{
        HttpSession session = request.getSession();
        session.removeAttribute("Account");
        System.out.println("==============userLoginOut成功退出登陆=============");
        return "redirect:toLoginUser";
    }

    /**
     * 退出登陆清除管理员session
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/adminLoginOut")
    public String adminLoginOut(HttpServletRequest request, HttpServletResponse response) throws Exception{
        HttpSession session = request.getSession();
        session.removeAttribute("Account");
        System.out.println("==============adminLoginOut成功退出登陆=============");
        return "redirect:toLoginAdmin";
    }


    public void addCookie(HttpServletResponse response, String name, String value){
        Cookie cookie = new Cookie(name,value);
        cookie.setMaxAge(1*365*24*60*60);
        cookie.setPath("/");
        response.addCookie(cookie);
        System.out.println("================添加cookie================");
    }
}
